package com.eliansun.crawler.pageparse.sichuan;

import com.eliansun.crawler.pageparse.AbstractPageProcess;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * user: lxc
 * data:2019- 07-04-09
 * 恩高科技     www.eycx.com
 */
public class Engao extends AbstractPageProcess {

    int count = 0;

    @Override
    public void process(Page page) {
        try {


            //列表界面
            if (page.getUrl().toString().indexOf("search") != -1) {
                List<String> nodes = page.getHtml().xpath("//div[@id=list-cont]//div[@class=item]//div[@class=img]//a/@href").all();
                if (nodes.size() == 0) {
                    return;
                }


                //列表界面解析出对应的详情url，放到队列中
                for (String node : nodes) {
                    page.addTargetRequest(node);
                }

                //获取下一页放到队列中
                String s = page.getHtml().xpath("//div[@class=pagebox]//a[@class=laypage-next]").links().toString();
                page.addTargetRequest(s);

            } else {
                //详情界面
                Map<String, Object> resMap = new HashMap<String, Object>(16);
                // 商品名称
                resMap.put("goodsName", page.getHtml().xpath("//div[@class=title]/h1/text()"));

                resMap.put("gg", page.getHtml().xpath("//table[@class=layui-table]/tbody//tr[1]//td[1]/text()"));

                //品牌
                resMap.put("pp", page.getHtml().xpath("//div[@class=product-param-item][2]//span[2]/text()"));

                //批准文号
                resMap.put("zch", page.getHtml().xpath("//div[@class=product-param-item][1]//span[2]/text()"));

                String price = page.getHtml().xpath("//p[@class=activity]/strong/text()").get();
                resMap.put("price", price.replace("￥", ""));

                //批准文号
//                resMap.put("zch",page.getHtml().xpath("//ul[@class=c]//li[5]//span/text()"));



                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put("spec", resMap.get("gg"));
                resultMap.put("medicalDeviceNo", resMap.get("zch"));
                resultMap.put("productCompany", resMap.get("pp"));
                resultMap.put("price", resMap.get("price"));
                resultMap.put("brand", resMap.get("pp"));
                resultMap.put("productName", resMap.get("goodsName"));
                resultMap.put("executiveStandard", "");
                resultMap.put("nationality", "");
                resultMap.put("store", super.cTerrace);
                resultMap.put("url", page.getUrl());
                resultMap.put("version", "");
                page.putField("result", resultMap);
                count++;
                System.out.println(count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    Site site = Site.me()
            //设置编码
            .setCharset("utf-8")
            //设置抓取间隔时间
            .setSleepTime(1000 * 3)
            //设置超时时间，单位毫秒
            .setTimeOut(1000 * 3)
            //设置重试间隔时间
            .setRetrySleepTime(1000 * 3)
            //设置重试次数
            .setRetryTimes(3);

    @Override
    public Site getSite() {
        return site;
    }


    public static void main(String[] args) {
        Spider.create(new Engao())
                // .addUrl("http://www.eycx.com/product/detail/65283/")
                .addUrl("http://www.eycx.com/product/search/?keyword=1")
                //设置多个线程同时抓取数据
                .thread(1)
                .run();

    }

}
